import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class TankClient extends Frame {

	public static final int GAME_WIDTH = 800;
	public static final int GAME_HEIGHT = 600;

	Tank tank = new Tank(50,50,true,this);
	Image offScreenImage = null;
	Wall wall = new Wall(200,300,250,30,this);
	Blood blood = new Blood();
	
	List<Missile> missiles = new ArrayList<Missile>();
	List<Tank> tanks = new ArrayList<Tank>();
	List<Explode> explodes = new ArrayList<Explode>();
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		tank.draw(g);
		wall.draw(g);
		blood.draw(g);
		tank.hitBoold(blood);
		for (int i = 0; i < explodes.size(); i++) {
			Explode e = explodes.get(i);
			e.draw(g);
		}
		for (int i = 0; i < tanks.size(); i++) {
			Tank btank = tanks.get(i);
			btank.draw(g);
			btank.hitTanks(tanks);
			btank.hitWall(wall);
		}
		for (int i = 0; i < missiles.size(); i++) {
			Missile m = missiles.get(i);
			m.hitTanks(tanks);
			m.draw(g);
			m.hitTank(tank);
			m.hitWall(wall);
		}
		g.drawString("missiles count:" + missiles.size(), 40, 40);
		g.drawString("tanks    count:" + tanks.size(), 150, 40);
		g.drawString("life     count:" + tank.getLife(), 260, 40);
		g.drawString("explodes count:" + explodes.size() , 370,40);
		if(0 ==tanks.size())
			for (int i = 0; i < 5; i++) {
				Tank m = new Tank(false,this);
				tanks.add(m);
			}
	}

	/**/@Override
	public void update(Graphics g) {
		// TODO Auto-generated method stub
		if (null == offScreenImage) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics goffScreen = offScreenImage.getGraphics();
		Color c = goffScreen.getColor();
		goffScreen.setColor(Color.GREEN);
		goffScreen.fillRect(0, 0, GAME_WIDTH,GAME_HEIGHT);
		g.setColor(c);
		paint(goffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	public void lauchFrame() {
		for (int i = 0; i < 5; i++) {
			Tank m = new Tank(false,this);
			tanks.add(m);
		}
		this.setLocation(550, 100);
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setTitle("Tank");
		this.setBackground(Color.GREEN);
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		this.addKeyListener(new KeyPressed());
		this.setResizable(false);
		this.setVisible(true);
		new Thread(new paintThread()).start();
	}

	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.lauchFrame();
	}

	private class paintThread implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (true) {
				repaint();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

	private class KeyPressed extends KeyAdapter {

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			tank.keyPressed(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			tank.keyReleased(e);
		}
		
	}
}
